<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>文章列表</title>
    <style type="text/css">
        #table table {
            width: 100%;
            font-size: 14px;
            border: 1px solid #eee
        }

        #table {
            padding: 0 10px;
        }

        table thead th {
            background: #f5f5f5;
            padding: 10px;
            text-align: left;
        }

        table tbody td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #eee;
            border-right: 1px solid #eee;
        }

        table tbody td span {
            margin: 0 10px;
            cursor: pointer;
        }

        .delete {
            color: red;
        }

        .edit {
            color: #008cd5;
        }

        .add {
            border: 1px solid #eee;
            margin: 10px 0;
            padding: 15px;
            text-align: center;
        }

        input {
            border: 1px solid #ccc;
            padding: 5px;
            border-radius: 3px;
            margin-right: 15px;
        }

        button {
            background: #008cd5;
            border: 0;
            padding: 4px 15px;
            border-radius: 3px;
            color: #fff;
        }

        #mask {
            background: rgba(0, 0, 0, .5);
            width: 100%;
            height: 100%;
            position: fixed;
            z-index: 4;
            top: 0;
            left: 0;
        }

        .mask {
            width: 300px;
            height: 250px;
            background: rgba(255, 255, 255, 1);
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            z-index: 47;
            border-radius: 5px;
        }

        .title {
            padding: 10px;
            border-bottom: 1px solid #eee;
        }

        .title span {
            float: right;
            cursor: pointer;
        }

        .content {
            padding: 10px;
        }

        .content input {
            width: 270px;
            margin-bottom: 15px;
        }
    </style>
</head>

<body>
    <div id="table">
        <div class="add">
            <input type="text" name="title" value="" placeholder="标题" />
            <input type="text" name="user" value="" placeholder="发布人" />
            <input type="date" name="date" value="" placeholder="发布时间" />
            <button class="xin">新增</button>
        </div>
        <table cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>标题</th>
                    <th>发布人</th>
                    <th>发布时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <!-- 需要在这里输出表格数据 -->
                <!-- <tr>
                    <td class="t_id"></td>
                    <td class="t_title"></td>
                    <td class="t_user"></td>
                    <td class="t_date"></td>
                    <td class="t_use">
                        <span>删除</span>
                        <span>编辑</span>
                    </td>
                </tr>
                -->
                
            </tbody>
        </table>
        <div id="mask">
            <div class="mask">
                <div class="title"> 编辑 <span class="oX"> X </span>
                </div>
                <div class="content">
                    <input type="text" name="title" value="" placeholder="标题" />
                    <input type="text" name="user" value="" placeholder="发布人" />
                    <input type="date" name="date" value="" placeholder="发布时间" />
                    <button class="_yes">更新</button>
                    <button class="_no">取消</button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        const arr = JSON.parse(window.localStorage.getItem('arr')) || []
        var addDate, addUser, addText, submit, tbody, mask, change
        var changeTitle, changeUser, changeDate;
        init();
        function init() {
            mask = document.querySelector('#mask');
            mask.addEventListener('click', maskclick)
            change = document.querySelector('.mask .change');
            tbody = document.querySelector('tbody');
            tbody.addEventListener('click', changeclick);
            add = document.querySelector('.add');
            changeTitle = document.querySelector('.changeTitle');
            changeUser = document.querySelector('.changeUser');
            changeDate = document.querySelector('.changeDate');
            addTitle = document.querySelector('input[name="title"]');
            addUser = document.querySelector('input[name="user"]');
            addDate = document.querySelector('input[name="date"]');
            submit = document.querySelector('.add button');
            submit.addEventListener('click', addclick);
            setPage();
        }
        //点击新增功能
        function addclick(e) {
            let Titleval = addTitle.value;
            let Userval = addUser.value;
            let Dateval = addDate.value;
            //有任意条件没写入不能生成数据进入表格
            if (!Titleval || !Userval || !Dateval) {
                alert('您输入的数据格式有误或不完整')
                return
            } else {
                //成功写入将新数据添加至表格
                const info = {
                    title: Titleval,
                    user: Userval,
                    date: Dateval,
                }
                arr.push(info);
            }
        setPage();
        }
        //渲染页面
        function setPage() {
            //如果没有数据   显示没有数据
            if (arr.length === 0) {
                tbody.innerHTML = '<tr><td colspan="7">没有数据</td></tr>';
                return;
            }
            //创建一个空字符串
            let str = '';
            //循环遍历动态渲染生成页面
            arr.forEach(function (item, key) {
                str +=
                    `
                    <tr>
                    <td>${key + 1}</td>
                    <td>${item.title}</td>
                    <td>${item.user}</td>
                    <td>${item.date}</td>
                    <td><button name="change" index=${key}>修改</button></td>
                    <td><button name="del" index=${key}>删除</button></td>
                    </tr>
                     `
            })
            tbody.innerHTML = str;
            window.localStorage.setItem('arr', JSON.stringify(arr));
        }
        //点击修改功能
        function changeclick(e) {
            if (e.target.getAttribute('name') === 'del') {
                if (!confirm('您确定要删除吗'))
                    return;
                arr.splice(e.target.getAttribute('index'), 1);
                setPage();
            } else if (e.target.getAttribute('name') === 'change') {
                mask.style.display = 'flex';
                index = Number(e.target.getAttribute('index'));
                changeTitle.value = arr[index].title;
                changeUser.value = arr[index].user;
                changeDate.value = arr[index].date;
            }
        }
        function maskclick(e) {
            if (e.target.getAttribute('class') === 'cancel'||e.target.nodeName === 'SPAN') {
                mask.style.display = 'none';
            } else if (e.target.getAttribute('class') === 'change') {
                if (confirm('是否确定修改')) {
                    let newUser = changeUser.value;
                    let newTitle = changeTitle.value;
                    let newDate = changeDate.value;
                    arr[index].title = newTitle;
                    arr[index].user = newUser;
                    arr[index].date = newDate;
                    if (!newUser || !newTitle || !newDate) {
                        alert('您输入的数据格式有误或不完整');
                        return;
                    }
                    setPage();
                    mask.style.display = 'none';
                }
            }
        }
    </script>
</body>
<!-- <script src="./js/index.js"></script> -->

</html>